import { defineStore } from 'pinia'
import { key } from './key';


interface IUserState {
    menus: IMenuModel[],
    username: string,
}


export const useUser = defineStore(key, {
    state(): IUserState {
        return {
            menus: [],
            username: "",
        };
    },
    getters: {
        routes() {
            type menu = IMenuModel & { children: IMenuModel[] }

            let menusdata: menu[] = [];
            // 将外层数组判断组合
            menusdata = this.menus.filter(item => item.parentId === 0).map((item) => ({
                ...item,
                children: []
            }))

            // 对外层数组修改,从总数组中筛选到内层数组内
            this.menus.forEach(sumitem => {
                menusdata.forEach(item => {
                    if (item.id === sumitem.parentId && item ) {
                        item.children.push(sumitem)
                    }
                })
            });
            return menusdata

        },
        Permissions(): string[] {
            return this.menus.map(item => item.name)
        }
    }
})